/*
* Copyright (c) 2020 - 2024 Renesas Electronics Corporation and/or its affiliates
*
* SPDX-License-Identifier: BSD-3-Clause
*/

/*******************************************************************************************************************//**
 * @addtogroup SCE
 * @{
 ***********************************************************************************************************************/

#ifndef HW_SCE_COMMON_H
#define HW_SCE_COMMON_H

/**********************************************************************************************************************
 * Includes
 ***********************************************************************************************************************/
#include "bsp_api.h"
#include "r_sce_if.h"

/***********************************************************************************************************************
 * Macro definitions
 ***********************************************************************************************************************/
#define R_SCE_BITS_TO_BYTES(x)     (((x) + 7U) >> 3U)
#define R_SCE_BYTES_TO_BITS(x)     ((x) << 3U)
#define R_SCE_WORDS_TO_BYTES(x)    ((x) << 2U)
#define R_SCE_BYTES_TO_WORDS(x)    (((x) + 3U) >> 2U)

/***********************************************************************************************************************
 * Typedef definitions
 ***********************************************************************************************************************/

/***********************************************************************************************************************
 * Private function prototypes
 ***********************************************************************************************************************/

/***********************************************************************************************************************
 * Private global variables
 ***********************************************************************************************************************/

/***********************************************************************************************************************
 * Private Functions
 ***********************************************************************************************************************/
uint32_t change_endian_long(uint32_t data);

/*******************************************************************************************************************//**
 *
 **********************************************************************************************************************/
__STATIC_INLINE void HW_SCE_PowerOn (void)
{
#if BSP_FEATURE_TRNG_HAS_MODULE_STOP

    // RA2 MCU series has separate power control for RNG
    R_MSTP->MSTPCRC_b.MSTPC28 = 0;
#endif

    // power on the SCE module
    R_MSTP->MSTPCRC_b.MSTPC31 = 0;
}

__STATIC_INLINE void HW_SCE_PowerOff (void)
{
#if BSP_FEATURE_TRNG_HAS_MODULE_STOP

    // Disable hardware TRNG before module stop.
    // Note: Must perform this step BEFORE TRNG module stop, or the TRNG hardware will draw excessive current in power off.
    R_TRNG->TRNGSCR0_b.SGCEN = 0;

    // RA2 MCU series has separate power control for RNG
    R_MSTP->MSTPCRC_b.MSTPC28 = 1;
#endif

    // power off the SCE module
    R_MSTP->MSTPCRC_b.MSTPC31 = 1;
}

#endif                                 /* HW_SCE_COMMON_H */

/*******************************************************************************************************************//**
 * @}
 **********************************************************************************************************************/
